package charts; 

import charts.series.Element;
import charts.series.dots.PointDotBase;
import charts.series.dots.Point;

import string.Utils;

import flash.display.BlendMode;
import flash.display.Sprite;
import flash.geom.Point;

import charts.series.dots.DefaultDotProperties;
import charts.Structs.AreaStyleStruct;

class Area extends LineBase<AreaStyleStruct, Element> {

	//var fill:String;

	public function new( json:Dynamic ) {
		super();
/*		
		style = {
			values:			[],
			width:			2,
			colour:			'#3030d0',
			fill:			'#3030d0',
			text:			'',		// <-- default not display a key
			'dot-size':		5,
			'font-size':	10,
			'fill-alpha':	0.6,
			'line-style':	new LineStyle( json['line-style'] ),
			loop:			false,		// <-- for radar charts
			
			// default dot:
			// HACK: fix this (remove the merge below)
			'--dot-style':	null
		};
*/
		var obj:AreaStyleStruct = {
			colour			: '#3030d0'
			, dot_size		: 5
			, font_size		: 10
			, fill			: '#3030d0'
			, fill_alpha		: 0.6
			, line_style		: new LineStyle( json['line-style'] )
			, loop			: false // <-- for radar charts
			, text			: ''		// <-- default not display a key
			, values		: []
			, width			: 2
			// default dot:
			// HACK: fix this (remove the merge below)
			, __dot_style		: null

		};

		style = obj;
		
		Object_helper.merge_2( json, style );

		// hack:
		style.__dot_style = new DefaultDotProperties(
			json['dot-style'], '#val#', this.style.colour);
		
		if( style.fill == '' )
			style.fill = style.colour;
			
		style.colour = string.Utils.get_colour( style.colour );
		style.fill = string.Utils.get_colour( style.fill );
		
		this.key = style.text;
		this.font_size = style.font_size;
		this.values = style.values;
		this.add_values();
		
		//
		// so the mask child can punch a hole through the line
		//
		this.blendMode = BlendMode.LAYER;
	}
	
	public function resize(sc:ScreenCoordsBase):Void {
		
		this.graphics.clear();
		// now draw the line + hollow dots
		super.resize(sc);
		
		var last:Element;
		var first:Bool = true;
		var spr:Sprite;
		
		for ( i in 0...this.numChildren) {
			
			spr = cast(this.getChildAt(i), Sprite);
			
			// filter out the masks
			if( Std.is( spr, Element) ) {
				
				var e:Element = cast( spr, Element);
				
				if( first )
				{
					
					first = false;
					
					if (style.loop)
					{
						// assume we are in a radar chart
						this.graphics.moveTo( e.x, e.y );
					}
					else
					{
						// draw line from Y=0 up to Y pos
						this.graphics.moveTo( e.x, sc.get_y_bottom(false) );
					}
					
					//
					// TO FIX BUG: you must do a graphics.moveTo before
					//             starting a fill:
					//
					this.graphics.lineStyle(0,0,0);
					this.graphics.beginFill( style.fill, style.fill_alpha );
					
					if (!this.style.loop)
						this.graphics.lineTo( e.x, e.y );
					
				}
				else
				{
					this.graphics.lineTo( e.x, e.y );
					last = e;
				}
			}
		}
		
		if ( last != null ) {
			if ( !style.loop) {
				this.graphics.lineTo( last.x, sc.get_y_bottom(false) );
			}
		}
		

		this.graphics.endFill();
	}
}
